package com.dw.etl.core.controller;

import com.dw.etl.common.base.BaseException;
import com.dw.etl.common.base.BaseResult;
import com.dw.etl.common.constant.CommonResultConstant;
import com.dw.etl.common.vo.PaginationVO;
import com.dw.etl.core.api.BaseSdService;
import com.dw.etl.core.bean.model.BaseSd;
import com.dw.etl.core.bean.model.MdMedTb;
import com.dw.etl.core.bean.vo.LayMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * baseSd控制器
 * Created by hozeData on 2021/05/21.
 */
@RestController
@RequestMapping("/etl/baseSd/")
@Api( tags="基础字典")
@CrossOrigin(origins = "*", maxAge = 3600)
public class BaseSdController {

    @Autowired
    BaseSdService baseSdService;

    @ApiOperation(value="分页查询")
    @RequestMapping(value="findByPage", method=RequestMethod.GET)
    public LayMsg findByPage(String str, Integer page, Integer limit) {
        PaginationVO<BaseSd> pageVO = baseSdService.findPageByModel(null, page, limit);
        return LayMsg.success().addCount(pageVO.getTotal()).addData(pageVO.getEntryList());
    }

    @ApiOperation(value="保存")
    @ApiImplicitParam(name="mdMedTb", value="实体", paramType="body", required=true, dataType="BaseSd")
    @RequestMapping(value="save", method=RequestMethod.POST)
    public BaseResult save(@RequestBody BaseSd mdMedTb, HttpServletRequest request)  {
        int num;
        if(StringUtils.isEmpty(mdMedTb.getIdSd())) {
            num = baseSdService.insertSelective(mdMedTb);
        } else {
            num = baseSdService.updateByIdSelective(mdMedTb);
        }
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="详情")
    @ApiImplicitParam(name="idSd", value="主键", paramType="query", required=true, dataType="Long")
    @RequestMapping(value="getById", method=RequestMethod.GET)
    public BaseResult getById(Long idSd, HttpServletRequest request) throws BaseException {
        BaseSd baseSd = baseSdService.getById(idSd);
        return new BaseResult(CommonResultConstant.SUCCESS, baseSd);
    }

    @ApiOperation(value="根据主键删除")
    @ApiImplicitParam(name="idSd", value="主键", paramType="query", required=true, dataType="Long")
    @RequestMapping(value="delete", method=RequestMethod.GET)
    public BaseResult delete(Long idSd, HttpServletRequest request) throws BaseException {
        int num = baseSdService.deleteById(idSd);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }



}
